which cl link yasm cpp

set -e

function build_libvecryptor()
{
    echo "===== Building libvecrptor ====="
	cd ${REPO}/libvecrptor
	
    cl  -I. -I$REPO -I$REPO/buildscripts/extra/ffmpeg -I$REPO/libvecrptor/compat/windows -FI./compat/windows/compat.h -O2 -Ob2 -Zi -MD -std:c++14 -utf-8 -c $REPO/libvecrptor/*.cpp
	link -dll -DEBUG -out:libvecrptor.dll -LIBPATH:$REPO/buildscripts/extra/ffmpeg/libavformat -LIBPATH:$REPO/buildscripts/extra/ffmpeg/libavutil avformat.lib avutil.lib -DEF:libvecrptor.def $REPO/libvecrptor/*.obj
		
    cd -
    echo "===== Building crptor libraries end ====="
}

#compile ffmpeg.dll for ffmpeg common line
function build_ffmpegDLL()
{
    echo "===== Building ffmpeg.dll ====="
	cd ${REPO}/libffmpeg
	
    dll_dir=${REPO}/libffmpeg
    echo ${dll_dir}

    cl  -I. -I$REPO -I$REPO/buildscripts/extra/ffmpeg -I$REPO/buildscripts/build/thirdparty/include \
        -I$REPO/compat/atomics/win32 \
        -O2 -Ob2 -Zi -MD -std:c11 -utf-8 -c $REPO/libffmpeg/*.c
        
        link -dll -DEBUG -out:ffmpeg.dll \
        -LIBPATH:$REPO/buildscripts/extra/ffmpeg/libavformat -LIBPATH:$REPO/buildscripts/extra/ffmpeg/libavutil \
        -LIBPATH:$REPO/buildscripts/extra/ffmpeg/libavcodec -LIBPATH:$REPO/buildscripts/extra/ffmpeg/libavfilter \
        -LIBPATH:$REPO/buildscripts/extra/ffmpeg/libswresample -LIBPATH:$REPO/buildscripts/extra/ffmpeg/libavdevice \
        -LIBPATH:$REPO/buildscripts/extra/ffmpeg/libswscale -LIBPATH:$REPO/libvecrptor \
        swresample.lib avdevice.lib avcodec.lib avfilter.lib avformat.lib avutil.lib  swscale.lib libvecrptor.lib \
        -DEF:ffmpeg.def $REPO/libffmpeg/*.obj

    cd -
    echo "===== Building ffmpeg.dll end ====="
}

function build_libffmpeg()
{
    echo "===== Building libffmpeg ====="
	cd ${REPO}/fftools

    dll_dir=$REPO/buildscripts/extra/ffmpeg
    echo ${dll_dir}

    cl  -I. -I$REPO -I$REPO/compat/atomics/dummy -I$REPO/buildscripts/extra/ffmpeg -I$REPO/buildscripts/build/thirdparty/include \
        -O2 -Ob2 -Zi -MD -std:c++14 -utf-8 \
        $REPO/fftools/ffmpeg.c $REPO/fftools/cmdutils.c $REPO/fftools/ffmpeg_opt.c \
        $REPO/fftools/ffmpeg_filter.c $REPO/fftools/ffmpeg_hw.c $REPO/fftools/proxy.c \
        $REPO/fftools/opt_common.c $REPO/fftools/ffmpeg_sched.c $REPO/fftools/ffmpeg_dec.c\
        $REPO/fftools/ffmpeg_mux.c $REPO/fftools/ffmpeg_demux.c $REPO/fftools/ffmpeg_mux_init.c\
        $REPO/fftools/ffmpeg_enc.c $REPO/fftools/thread_queue.c $REPO/fftools/sync_queue.c\
        $REPO/fftools/objpool.c\
        user32.lib kernel32.lib \
        $dll_dir/libavformat/avformat.lib $dll_dir/libavutil/avutil.lib \
        $dll_dir/libavcodec/avcodec.lib $dll_dir/libavfilter/avfilter.lib \
        $dll_dir/libswresample/swresample.lib $dll_dir/libavdevice/avdevice.lib \
        $dll_dir/libswscale/swscale.lib ${REPO}/libvecrptor/libvecrptor.lib

    cd -
    echo "===== Building libffmpeg end ====="
}

function update_rc()
{
    echo "===== Update version and set open gen rc ====="
    cd ${REPO}

    if test "$FFmpeg_Version"; then
        export revision=${FFmpeg_Version}
    else
        export revision=1.0.0
    fi
    export extra_version=$(git rev-parse --short HEAD)
    export HAVE_GNU_WINDRES=yes

    echo revision:${revision}
    echo extra_version:${extra_version}
    echo HAVE_GNU_WINDRES:${HAVE_GNU_WINDRES}

    cd -
    echo "===== Update version and set open gen rc end ====="
}

# Build FFmpeg
echo "===== Building FFmpeg ====="

echo FF_CONFIG_TYPE:$FF_CONFIG_TYPE
source ff_config_win.sh
echo COMMON_FF_CFG_FLAGS:$COMMON_FF_CFG_FLAGS

cd extra/
if [ ! -z "$BUILD_ALL_FROM_SOURCE" ]; then
    [ -d ffmpeg ] && rm -rf ffmpeg
fi
[ -d ffmpeg ] || mkdir ffmpeg

cd ffmpeg
export PKG_CONFIG_PATH=../../build/thirdparty/lib-x64/pkgconfig:$PKG_CONFIG_PATH
export PKG_CONFIG_PATH=../../build/ffnvcodec/pkgconfig:$PKG_CONFIG_PATH
export PKG_CONFIG_PATH=../../build/mfx/pkgconfig:$PKG_CONFIG_PATH
export PKG_CONFIG_PATH=../../build/speex/pkgconfig:$PKG_CONFIG_PATH
export PKG_CONFIG_PATH=../../build/jxl/pkgconfig:$PKG_CONFIG_PATH
echo $PKG_CONFIG_PATH

echo "start configure"

REPO=$(git rev-parse --show-toplevel)
cur_dir=$(pwd)
echo ${cur_dir}
cd ${REPO}
cd ${cur_dir}
pwd

# genrate version and open gen rc
update_rc

$REPO/configure --toolchain=msvc --target-os=win64 \
    --arch=x86_64 \
    --stdc=c11 \
    $COMMON_FF_CFG_FLAGS \
    --prefix=../../build/ffmpeg-x64 \
    --extra-cflags="-I../../build/amf/include -I../../build/thirdparty/include/ -I../../build/mfx/include/ -I../../build/ffnvcodec/include/ -I../../build/speex/include/ -I../../build/jxl/include/ -MD"\
    --extra-ldflags="-LIBPATH:../../build/thirdparty/lib-x64/ -LIBPATH:../../build/mfx/lib/x64/ -LIBPATH:../../build/speex/lib/x64/ -LIBPATH:../../build/jxl/lib/"\

make -j 12
make install

# Build libvecryptor
build_libvecryptor
# Build ffmpeg.dll
build_ffmpegDLL
# Build libffmpeg
build_libffmpeg

cd ../../build
cp thirdparty/lib-x64/*.dll ffmpeg-x64/bin/
cp ../extra/ffmpeg/config.h ffmpeg-x64/include/
#move jxl.dll
cp jxl/lib/*.dll ffmpeg-x64/bin/

#copy other head file
#libavcodec
cp ../../libavcodec/packet_internal.h ffmpeg-x64/include/libavcodec/
cp ../../libavcodec/bytestream.h ffmpeg-x64/include/libavcodec/
cp ../../libavcodec/get_bits.h ffmpeg-x64/include/libavcodec/
cp ../../libavcodec/h264.h ffmpeg-x64/include/libavcodec/
cp ../../libavcodec/h2645_parse.h ffmpeg-x64/include/libavcodec/
cp ../../libavcodec/vlc.h ffmpeg-x64/include/libavcodec/
cp ../../libavcodec/mathops.h ffmpeg-x64/include/libavcodec/
cp ../../libavcodec/golomb.h ffmpeg-x64/include/libavcodec/
cp ../../libavcodec/hevc.h ffmpeg-x64/include/libavcodec/
cp ../../libavcodec/put_bits.h ffmpeg-x64/include/libavcodec/
cp ../../libavcodec/audio_frame_queue.c ffmpeg-x64/include/libavcodec/
cp ../../libavcodec/audio_frame_queue.h ffmpeg-x64/include/libavcodec/
cp ../../libavcodec/thread.h ffmpeg-x64/include/libavcodec/
cp ../../libavcodec/hevc_ps.h ffmpeg-x64/include/libavcodec/
cp ../../libavcodec/internal.h ffmpeg-x64/include/libavcodec/
cp ../../libavcodec/smartcodec.h ffmpeg-x64/include/libavcodec/
cp ../../libavcodec/refstruct.h ffmpeg-x64/include/libavcodec/
cp ../../libavcodec/decode.h ffmpeg-x64/include/libavcodec/

mkdir ffmpeg-x64/include/libavcodec/x86
cp ../../libavcodec/x86/mathops.h ffmpeg-x64/include/libavcodec/x86/
cp ../../libavcodec/rnd_avg.h ffmpeg-x64/include/libavcodec/
cp ../../libavcodec/hevcpred.h ffmpeg-x64/include/libavcodec/
cp ../../libavcodec/hevcdec.h ffmpeg-x64/include/libavcodec/
cp ../../libavcodec/dovi_rpu.h ffmpeg-x64/include/libavcodec/
cp ../../libavcodec/h274.h ffmpeg-x64/include/libavcodec/
cp ../../libavcodec/threadframe.h ffmpeg-x64/include/libavcodec/
cp ../../libavcodec/bswapdsp.h ffmpeg-x64/include/libavcodec/
cp ../../libavcodec/cabac.h ffmpeg-x64/include/libavcodec/
cp ../../libavcodec/h2645_parse.h ffmpeg-x64/include/libavcodec/
cp ../../libavcodec/hevc.h ffmpeg-x64/include/libavcodec/
cp ../../libavcodec/hevc_ps.h ffmpeg-x64/include/libavcodec/
cp ../../libavcodec/hevcdsp.h ffmpeg-x64/include/libavcodec/
cp ../../libavcodec/hevc_parse.h ffmpeg-x64/include/libavcodec/
cp ../../libavcodec/hevc_data.h ffmpeg-x64/include/libavcodec/
cp ../../libavcodec/h2645_vui.h ffmpeg-x64/include/libavcodec/
cp ../../libavcodec/h2645_sei.h ffmpeg-x64/include/libavcodec/
cp ../../libavcodec/aom_film_grain.h ffmpeg-x64/include/libavcodec/
cp ../../libavcodec/sei.h ffmpeg-x64/include/libavcodec/
cp ../../libavcodec/codec_internal.h ffmpeg-x64/include/libavcodec/
cp ../../libavcodec/hevc_sei.h ffmpeg-x64/include/libavcodec/
cp ../../libavcodec/parser.h ffmpeg-x64/include/libavcodec/
cp ../../libavcodec/videodsp.h ffmpeg-x64/include/libavcodec/
cp ../../libavcodec/golomb.c ffmpeg-x64/include/libavcodec/
	
mkdir ffmpeg-x64/include/libpostproc
cp ../../libpostproc/postprocess.h ffmpeg-x64/include/libpostproc/
cp ../../libpostproc/version.h ffmpeg-x64/include/libpostproc/
cp ../../libpostproc/version_major.h ffmpeg-x64/include/libpostproc/

#compat
mkdir ffmpeg-x64/include/compat
cp ../../compat/va_copy.h ffmpeg-x64/include/compat/
cp ../../compat/w32pthreads.h ffmpeg-x64/include/compat/
cp ../../compat/w32dlfcn.h ffmpeg-x64/include/compat/
mkdir ffmpeg-x64/include/compat/atomics
mkdir ffmpeg-x64/include/compat/atomics/win32
cp ../../compat/atomics/win32/stdatomic.h ffmpeg-x64/include/compat/atomics/win32/

#libavformat
cp ../../libavformat/os_support.h ffmpeg-x64/include/libavformat/
cp ../../libavformat/isom.h ffmpeg-x64/include/libavformat/
cp ../../libavformat/dv.h ffmpeg-x64/include/libavformat/
cp ../../libavformat/internal.h ffmpeg-x64/include/libavformat/
cp ../../libavformat/mux.h ffmpeg-x64/include/libavformat/

#libavutil
cp ../../libavutil/internal.h ffmpeg-x64/include/libavutil/
cp ../../libavutil/attributes_internal.h ffmpeg-x64/include/libavutil/
cp ../../libavutil/libm.h ffmpeg-x64/include/libavutil/
cp ../../libavutil/reverse.h ffmpeg-x64/include/libavutil/
cp ../../libavutil/thread.h ffmpeg-x64/include/libavutil/
cp ../../libavutil/timer.h ffmpeg-x64/include/libavutil/
cp ../../libavutil/wchar_filename.h ffmpeg-x64/include/libavutil/
cp ../../libavutil/mem_internal.h ffmpeg-x64/include/libavutil/
cp ../../libavutil/getenv_utf8.h ffmpeg-x64/include/libavutil/

mkdir ffmpeg-x64/include/libavutil/x86
cp ../../libavutil/x86/asm.h ffmpeg-x64/include/libavutil/x86/
cp ../../libavutil/x86/timer.h ffmpeg-x64/include/libavutil/x86/
#libswresample
cp ../../libswresample/swresample_internal.h ffmpeg-x64/include/libswresample/

#libvecrptor
cp ../../libvecrptor/libvecrptor.dll ffmpeg-x64/bin/
cp ../../libvecrptor/libvecrptor.lib ffmpeg-x64/bin/

#libffmpeg
cp ../../fftools/ffmpeg.exe ffmpeg-x64/bin/

mkdir ffmpeg-x64/pdb
cp ../../fftools/ff*.pdb ffmpeg-x64/pdb
cp ../extra/ffmpeg/lib*/*.pdb ffmpeg-x64/pdb
#libvecrptor
cp ../../libvecrptor/libvecrptor.pdb ffmpeg-x64/pdb/

#libffmpeg(ffmpeg.dll)
mkdir ffmpeg-x64/include/libffmpeg
cp ../../libffmpeg/*.h ffmpeg-x64/include/libffmpeg/

cp ../../libffmpeg/ffmpeg.dll ffmpeg-x64/bin/
cp ../../libffmpeg/ffmpeg.lib ffmpeg-x64/bin/
cp ../../libffmpeg/ffmpeg.pdb ffmpeg-x64/pdb/
mv ffmpeg-x64 ../../